﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DAL;
using Model;
using MySql.Data.MySqlClient;

/// <summary>
/// 图书查询功能模块的业务逻辑层BLL
/// </summary>

namespace BLL
{
    public class BookServices
    {
        public static DataTable GetBookList(int startRow,int pageCount)
        {
            string sql = "select * from t_books order by BookId asc";
            return sqlHelper.ExcuteTable(sql,startRow,pageCount);//调用DAL层的sqlHelper类里的ExcuteTable（用到了DAL层）
        }

        public static int GetBookCount()
        {
            string sql = "select count(*) from t_books";
            return Convert.ToInt32(sqlHelper.ExcuteScalar(sql,null));
        }

        public static bool AddBook(Model.Book b)
        {
            string sql = @"insert into t_books(BookId,Name,Publiser,Price,BorrowCount)values(@BookId,@Name,@Publiser,@Price,@BorrowCount)";
            MySqlParameter[] parameters = new MySqlParameter[5] { new MySqlParameter("BookId", b.BookId), new MySqlParameter("Name", b.Name), new MySqlParameter("Publiser", b.Publiser), new MySqlParameter("Price", b.Price), new MySqlParameter("BorrowCount", b.BorrowCount) };
            if (sqlHelper.ExcuteNonquery(sql, parameters) > 0)
                return true;
            else
                return false;
        }

        public static bool DeleteBook(Model.Book b)
        {
            string sql = @"delete from t_books where Name=@Name";
            MySqlParameter[] parameters = new MySqlParameter[1] { new MySqlParameter("Name",b.Name)};
            if (sqlHelper.ExcuteNonquery(sql, parameters) > 0)
                return true;
            else
                return false;
        }

        public static DataTable SearchBook(Model.Book b)
        {
       
            string sql = @"select * from t_books where Name=@Name";
            MySqlParameter[] parameters = new MySqlParameter[1] { new MySqlParameter("Name", b.Name) };
            return sqlHelper.ExcuteTable(sql,parameters);
        }

        public static bool ModifyBook(Book b)
        {
            string sql = @"update t_books set Price=@Price,BorrowCount=@BorrowCount where Name=@Name";
            MySqlParameter[] parameters = new MySqlParameter[3] {new MySqlParameter("Name", b.Name),new MySqlParameter("Price", b.Price), new MySqlParameter("BorrowCount", b.BorrowCount) };
            if (sqlHelper.ExcuteNonquery(sql, parameters) > 0)
                return true;
            else
                return false;
        }
    }
}
